#include "RdfXmlHandler.h"
#include "dal/XmlUtil.h"
#include <list>
#include <iostream>

namespace xpm {
	void RdfXmlHandler::Parse() {
		DOMElement* xmlRoot = _doc->getDocumentElement();
		DOMNamedNodeMap* map = xmlRoot->getAttributes();
		DOMNode* node = NULL;
	}

	void RdfXmlHandler::Serialize() {		
		InstancesMap map = _project->knowledgeBase()->instances();
		for(InstancesIter it = map.begin(); it != map.end(); ++it) {
			//create element
			string elename = _baseName + ":" + it->second->cls()->name();
			DOMElement* nodeElement = _doc->createElement(X(elename.c_str()));			
			_doc->getDocumentElement()->appendChild(nodeElement);
			//create attributes
			DOMNamedNodeMap* attrs = nodeElement->getAttributes();
			DOMAttr* attrAbout = _doc->createAttribute(L"rdf:about");
			DOMEntityReference* baseEntityEX = _doc->createEntityReference(X(_baseNameEX.c_str()));
			attrAbout->appendChild(baseEntityEX);
			DOMText* textAbout = _doc->createTextNode(X(it->second->name().c_str()));
			attrAbout->appendChild(textAbout);
			attrs->setNamedItem(attrAbout);

			DOMElement* eleLabel = _doc->createElement(L"rdfs:label");
			nodeElement->appendChild(eleLabel);
			//attrLabel->setPrefix(L"rdfs");
			eleLabel->setTextContent(X(it->first.c_str()));

			cout << "created the label :" << it->first << endl;

			SlotValueMap valuemap = it->second->slotValue();
			for(SlotValueIter itr = valuemap.begin(); itr != valuemap.end(); ++itr) {
				AppendSlotValue(nodeElement, itr->first,  itr->second);
			}

			cout << "created slot value." << endl;

			SlotValuesMap valuesmap = it->second->slotValues();
			for(SlotValuesIter itr = valuesmap.begin(); itr != valuesmap.end(); ++itr) {
				for(vector<string>::iterator vitr = itr->second.begin(); vitr != itr->second.end(); ++vitr) {
					AppendSlotValue(nodeElement, itr->first, *vitr);
				}
			}
		}
	}

	void RdfXmlHandler::AppendSlotValue(DOMNode* node, Slot* slot, string value) {
		//create element
		string elename = _baseName + ":" + slot->name();
		DOMElement* slotElement = _doc->createElement(X(elename.c_str()));			
		node->appendChild(slotElement);

		cout << "created the slot lement. " << endl;
		if(slot->valueType() != ValueType::STRING) {
			//create attributes
			DOMNamedNodeMap* attrsSlot = slotElement->getAttributes();
			DOMAttr* attrDatatype = _doc->createAttribute(L"rdf:datatype");
			DOMEntityReference* xsdEntity = _doc->createEntityReference(L"xsd");
			attrDatatype->appendChild(xsdEntity);
			DOMText* textXsd = NULL;
			switch(slot->valueType()) {
			case INTEGER: {
				textXsd = _doc->createTextNode(L"integer");
				break;
						  }
			case BOOLEAN: {
				textXsd = _doc->createTextNode(L"boolean");
				break;
						  }
			case FLOAT: {
				textXsd = _doc->createTextNode(L"float");
				break;
						}
			case SYMBOL: {
				textXsd = _doc->createTextNode(L"symbol");
				break;
						 } 
			}					
			attrDatatype->appendChild(textXsd);
			attrsSlot->setNamedItem(attrDatatype);
		}
		slotElement->setTextContent(X(value.c_str()));
	}
}